<?php
	function saveGoalIntoDatabase($goalname, $description, $startvalue, $value, $targetvalue, $priority, $measurename, $measuretype, $deadline, $userid){
		mysql_connect("localhost", "web964","pass17") or die ("Connection failed.");
		mysql_select_db("usr_web964_1") or die ("Database does not exist.");	
		
		
		$result = mysql_query(
		"INSERT INTO goal (name, description, startvalue, value, targetvalue, priority, measurename, fk_userid, fk_measuretypeid)
		 VALUES ('$goalname',
		 '$description',
		 '$startvalue',
		 '$value',
		 '$targetvalue',
		 '$priority',
		 '$measurename',
		 '$userid',
		 '$measuretype')")
		 or die ("MySQL-Error: " . mysql_error());
		 
		 $goalid = "";
		 $result2 = mysql_query(
		"SELECT id FROM  goal WHERE name = '$goalname'")
		 or die ("MySQL-Error: " . mysql_error());
		 while($row = mysql_fetch_object($result2)){
		 	$goalid = $row->id;
		 }
		 
		 if (preg_match('/\b(19|20)?[0-9]{2}[-](0?[1-9]|1[012])[-](0?[1-9]|[12][0-9]|3[01])\b/', $deadline)){
			$result3 = mysql_query(
			"INSERT INTO goaldeadline (date, fk_goalid)
			VALUES ('$deadline', '$goalid')")
			or die ("MySQL-Error: " . mysql_error());
			
		 }	
		
		# add subgoals into database
		
		$numberOfsubgoals = intval((sizeof($_POST) - 9 )/ 8);
		
		for ($i = 0 ; $i < $numberOfsubgoals ; $i++){
			
			$tempColumnName = "subgoalname_"."$i";			
			$subgoalname = $_POST[$tempColumnName];
			$tempColumnName = "subgoalstartvalue_"."$i";
			$subgoalstartvalue = $_POST[$tempColumnName];
			$tempColumnName = "subgoalcurrentvalue_"."$i";
			$subgoalcurrentvalue = $_POST[$tempColumnName];
			$tempColumnName = "subgoaltargetvalue_"."$i" ;
			$subgoaltargetvalue = $_POST[$tempColumnName];
			$tempColumnName = "subgoalpriority_"."$i";
			$subgoalpriority = $_POST[$tempColumnName];
			$tempColumnName = "subgoalmeasurename_"."$i";
			$subgoalmeasurename = $_POST[$tempColumnName];
			$tempColumnName = "subgoaldeadline_"."$i";
			$subgoaldeadline = $_POST[$tempColumnName];
			$tempColumnName = "subgoalmeasuretype_"."$i";
			$subgoalmeasuretype = $_POST[$tempColumnName];
			
			
			switch ($subgoalpriority) {
    			case "ui":
        			$subgoalpriority = "urgent / important";
        			break;
    			case "uni":
        			$subgoalpriority = "urgent / unimportant";
        			break;
	   			case "nui":
	        			$subgoalpriority = "unurgent / important";
	       			break;
	       		case "nuni":
	        			$subgoalpriority = "unurgent / unimportant";
	       			break;
			}
			
			$result4 = mysql_query("INSERT INTO subgoal (name, startvalue, value, targetvalue, priority, measurename, fk_goalid, fk_measuretypeid)
		 	VALUES ('$subgoalname',
		 	'$subgoalstartvalue',
		 	'$subgoalcurrentvalue',
		 	'$subgoaltargetvalue',
		 	'$subgoalpriority',
		 	'$subgoalmeasurename',
		 	".$goalid.",
		 	".$subgoalmeasuretype.")")
		 	or die ("MySQL-Error: " . mysql_error());
		 	
			if ($subgoaldeadline != ""){
				
			 	#get id of just saved subgoal
			 	$subgoalid = "";
			 	$result5 = mysql_query("SELECT id FROM subgoal WHERE fk_goalid=".$goalid." AND name='".$subgoalname."'") or die ("MySQL-Error: " . mysql_error());
				while($row = mysql_fetch_object($result5)){
			 		$subgoalid = $row->id;
			 	}
				$result6 = mysql_query("INSERT INTO subgoaldeadline (date, fk_subgoalid) VALUES ('$subgoaldeadline', '$subgoalid')") or die ("MySQL-Error: " . mysql_error());
		 	}
		 	
		}
	}
	
?>